/**
 * 
 * 
 */
//var server_url = "http://localhost:8888/cantho-gis/services/Services?wsdl";
var server_url = "http://172.16.65.21:8888/cantho-gis/services/Services?wsdl";
//var db_server = "172.16.65.21";
//var default_server = "172.16.65.21";
var default_server = "172.16.65.21";
var default_database = "postgis";

var db_server = default_server;
var db_name = default_database;

function isLogin(user, pwd, server, db){		
	var soapMessage = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:map=\"http://map\">";
    soapMessage += "<soapenv:Header/>";
    soapMessage += "<soapenv:Body>";
    soapMessage += "<map:isLogin>";         
    soapMessage += "     <map:username>" + user + "</map:username>";         
    soapMessage += "     <map:password>" + pwd + "</map:password>";
	soapMessage += "<map:server>"+ server +"</map:server>";
    soapMessage += "<map:database>" + db + "</map:database>";	
    soapMessage += "  </map:isLogin>";
    soapMessage += "</soapenv:Body>";	
    soapMessage += "</soapenv:Envelope>";
	
	//alert(soapMessage);
	$.ajax({
		type : "POST",
		url : server_url,
		cache : false,
		success : callBackIsLogin,
		error : errorIsLogin,
		dataType : "xml",// kieu du lieu tra ve (response)
		contentType : "text/xml; charset=\"utf-8\"", // kieu du lieu gui di
		//(request)
		data : soapMessage		
	});	
};


function callBackIsLogin(xml_result, status) {		
	//dat lai gia tri moi cho user_type
	user_type = xml_result.getElementsByTagName("ns:return")[0].childNodes[0].nodeValue * 1;	
	//alert("user_type: "+user_type);
	if(user_type < 3){
		//neu la server hien tai thi xoa het cac lop cu
		if(db_server == default_server){
			removeLayers();			
			getLayerList(db_server, db_name, user_name, pass_word);			
		}else{
			getLayerListFromOrtherDB(db_server, db_name, user_name, pass_word);					
		}		
		
		//Doi button thanh chu logout	
		$("#ext-gen15").attr('value','Logout');		
	}else{
		user_name = '';
		pass_word = '';
	}
}

function errorIsLogin(textStatus) {
	alert('Error: '+ textStatus);		
}

function getLayerList(server, database, user, pwd){	
	
	var soapMessage = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:map=\"http://map\">";
    soapMessage += "<soapenv:Header/>";
    soapMessage += "<soapenv:Body>";    
    soapMessage += "  <map:getLayerList>";
    soapMessage += "     <map:server>" + server + "</map:server>";
    soapMessage += "     <map:database>" + database + "</map:database>";
    soapMessage += "	 <map:username>" + user + "</map:username>";   
    soapMessage += "     <map:password>" + pwd + "</map:password>";
    soapMessage += "  </map:getLayerList>";
	soapMessage += "</soapenv:Body>";	
    soapMessage += "</soapenv:Envelope>";
	
 	$.ajax({
		type : "POST",
		url : server_url,
		cache : false,
		success : callBackGetLayerList,
		error : errorGetLayerList,
		dataType : "xml",// kieu du lieu tra ve (response)
		contentType : "text/xml; charset=\"utf-8\"", // kieu du lieu gui di
		//(request)
		data : soapMessage		
	});	
};

function callBackGetLayerList(xml_result, status) {	
	//xoa noi dung trong mang
	layer_db_arr.splice(0, layer_db_arr.length);
	var tenlop = "";
	var chiase = "";
	var n = xml_result.getElementsByTagName('ns:return').length;	
	for (i = 0; i < n; i++){
		tenlop = xml_result.getElementsByTagName('ns:return')[i].childNodes[0].childNodes[0].nodeValue;
		//chiase = xml_result.getElementsByTagName('ns:return')[i].childNodes[1].childNodes[0].nodeValue;		
		layer_db_arr[i] = tenlop;				
	}		
	
	if(db_server == default_server){			
			//getLayerList(db_server, db_name, user_name, pass_word);
			getLayerFromGeoServer();
	}		
	/*
	for(i = 0; i < n; i++){
		alert(layer_db_arr[i]);
	}
	*/
}

function errorGetLayerList(textStatus) {
	alert('getLayerList is Error: '+ textStatus);	
	//console.error(textStatus);	
}

function getLayerByName(layerName, server, database){	

	//alert('getLayerName: layername: ' + layerName + '- server: ' + server + '- database: ' + database);	
	var soapMessage = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:map=\"http://map\">";
    soapMessage += "<soapenv:Header/>";
    soapMessage += "<soapenv:Body>";    
    soapMessage += "   <map:getLayerByName>";
    soapMessage += "     <map:layer_name>" + layerName + "</map:layer_name>";
    soapMessage += "     <map:server>" + server + "</map:server>";
    soapMessage += "     <map:database>" + database + "</map:database>";
    soapMessage += "  </map:getLayerByName>";
	soapMessage += "</soapenv:Body>";	
    soapMessage += "</soapenv:Envelope>";
	//alert(soapMessage);
 	$.ajax({
		type : "POST",
		url : server_url,
		cache : false,
		success : callBackGetLayerByName,
		error : errorGetLayerByName,
		dataType : "xml",// kieu du lieu tra ve (response)
		contentType : "text/xml; charset=\"utf-8\"", // kieu du lieu gui di
		//(request)
		data : soapMessage
		//async: false
	});	
};


function callBackGetLayerByName(xml_result, status){	
	var new_layer_name = "";
	var wkt_format = new OpenLayers.Format.WKT();
	styleMap = new OpenLayers.StyleMap(new OpenLayers.Style({
			
	}))
	/***********Khai bao lop moi***************/
	//Lay ten lop: chinh la item dau tien cua ket qua tra ve
	
	new_layer_name = xml_result.getElementsByTagName('ns:return')[0].childNodes[0].childNodes[0].nodeValue;
	
	var newLayer = new OpenLayers.Layer.Vector(new_layer_name, {styleMap : ''});
	//alert("So luong feature: " + xml_result.getElementsByTagName('ns:return').length);
	for (i = 1; i < xml_result.getElementsByTagName('ns:return').length; i++) {
		wkt = xml_result.getElementsByTagName('ns:return')[i].childNodes[0].childNodes[0].nodeValue;		
		newLayer.addFeatures(wkt_format.read(wkt));
	}				
	newLayer.setVisibility(false);
	map.addLayers([newLayer]);		
	//Them lop moi vao danh sach layers 
	layer_arr_init[layer_arr_init.length] = newLayer;
}

function errorGetLayerByName(textStatus) {
	alert('getLayerByName is Error: '+ textStatus);	
	//console.error(textStatus);	
}


function getLayerListFromOrtherDB(server, database, user, pwd){	
	
	var soapMessage = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:map=\"http://map\">";
    soapMessage += "<soapenv:Header/>";
    soapMessage += "<soapenv:Body>";    
    soapMessage += "  <map:getLayerList>";
    soapMessage += "     <map:server>" + server + "</map:server>";
    soapMessage += "     <map:database>" + database + "</map:database>";
    soapMessage += "	 <map:username>" + user + "</map:username>";   
    soapMessage += "     <map:password>" + pwd + "</map:password>";
    soapMessage += "  </map:getLayerList>";
	soapMessage += "</soapenv:Body>";	
    soapMessage += "</soapenv:Envelope>";
	
 	$.ajax({
		type : "POST",
		url : server_url,
		cache : false,
		success : callBackGetLayerListFromOrtherDB,
		error : errorGetLayerList,
		dataType : "xml",// kieu du lieu tra ve (response)
		contentType : "text/xml; charset=\"utf-8\"", // kieu du lieu gui di
		//(request)
		data : soapMessage		
	});	
};

function callBackGetLayerListFromOrtherDB(xml_result, status) {	
	//xoa noi dung trong mang
	//layer_db_arr.splice(0, layer_db_arr.length);
	//alert(layer_db_arr.length);
	//layer_db_arr = new Array();
	var tenlop = "";
	var chiase = "";
	var n = xml_result.getElementsByTagName('ns:return').length;	
	for (i = 0; i < n; i++){
		tenlop = xml_result.getElementsByTagName('ns:return')[i].childNodes[0].childNodes[0].nodeValue;
		//chiase = xml_result.getElementsByTagName('ns:return')[i].childNodes[1].childNodes[0].nodeValue;		
		//layer_db_arr[i] = tenlop;	
		getLayerByName(tenlop, db_server, db_name);
	}		
	/*
	for(i = 0; i < n; i++){
		alert(layer_db_arr[i]);
	}
	*/
}

function errorGetLayerList(textStatus) {
	alert('getLayerList is Error: '+ textStatus);	
	//console.error(textStatus);	
}

function getSoNganh(server, database){		
	var soapMessage = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:map=\"http://map\">";
    soapMessage += "<soapenv:Header/>";
    soapMessage += "<soapenv:Body>";
	soapMessage += "      <map:getSoNganh>";
    soapMessage += "     <map:server>" + server + "</map:server>";
    soapMessage += "     <map:database>" + database + "</map:database>";
	soapMessage += " <map:current_server>" + default_server + "</map:current_server>";
    soapMessage += "  </map:getSoNganh>";
    soapMessage += "</soapenv:Body>";	
    soapMessage += "</soapenv:Envelope>";
	
	//alert(soapMessage);
	$.ajax({
		type : "POST",
		url : server_url,
		cache : false,
		success : callBackGetSoNganh,
		error : errorGetSoNganh,
		dataType : "xml",// kieu du lieu tra ve (response)
		contentType : "text/xml; charset=\"utf-8\"", // kieu du lieu gui di
		//(request)
		data : soapMessage		
	});	
};


function callBackGetSoNganh(xml_result, status) {		
	var masn = "";
	var tensn = "";
	var url = "";
	var n = xml_result.getElementsByTagName('ns:return').length;	
	$("#dialog" ).dialog( "open");
	//Xoa du lieu cu trong table
	$("#table-songanh").empty();
	for (i = 0; i < n; i++){
		masn = xml_result.getElementsByTagName('ns:return')[i].childNodes[0].childNodes[0].nodeValue;
		tensn = xml_result.getElementsByTagName('ns:return')[i].childNodes[1].childNodes[0].nodeValue;
		url = xml_result.getElementsByTagName('ns:return')[i].childNodes[2].childNodes[0].nodeValue;	
		$('#table-songanh').append('<tr><td><input class="chk_songanh" type="checkbox" value='+url+'></td><td>'+masn+'</td><td>'+tensn+'</td></tr>');
	}		
}

function errorGetSoNganh(textStatus) {
	alert('Error: '+ textStatus);		
	//$("#dialog" ).dialog( "open");
}